package isnork.g2;

import java.awt.geom.Point2D;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import isnork.g2.utility.Utility;
import isnork.sim.Observation;
import isnork.sim.SeaLifePrototype;
import isnork.sim.GameObject.Direction;

public class ObservationBasedDangerAvoidance implements Strategy {

	@Override
	public Map<Direction, Double> process(G2Explorer diver) {
		System.out.printf("Danger avoidance process started\n");
		Map<Direction, Double> utility_accumulators = new HashMap<Direction, Double>();
		for (Direction d : Direction.values())
			utility_accumulators.put(d, 0.0);

		try {
			for (Observation_Ext obs : diver.obs_ext) {
				double distance = diver.location.distance(obs.location);

				if (obs.prototype.isDangerous()) {
					// case 1. static dangerous creatures
					if (obs.prototype.getSpeed() == 0) {
						// Danger Avoidance for Static Creature
						double utility = -ExplorerConfig.c_ds
								* obs.happiness
								/ (1 + distance);

						// update utility accumulator for relative direction
						Direction relativeDir = Utility.RelativeDirection(
								diver.location, obs.location);
						double u = utility_accumulators.get(relativeDir);
						utility_accumulators.put(relativeDir, (u + utility));
						}
					}
					// case 2. dynamic dangerous creatures
					else { // Danger Avoidance for Static Creature
						double utility = -ExplorerConfig.c_do
								* obs.happiness
								/ (1 + distance);

						// update utility accumulator for relative direction
						Direction relativeDir = Utility.RelativeDirection(
								diver.location, obs.location);
						double u = utility_accumulators.get(relativeDir);
						utility_accumulators.put(relativeDir, (u + utility));
				}
			}
		} catch (Exception e) {
			e.printStackTrace(System.err);
		}
		
		
		java.awt.geom.Point2D boat = new java.awt.geom.Point2D.Double(0,0);
		for (Direction dir:Direction.values())
		{
			Point2D dest = new Point2D.Double(diver.location.getX()+dir.getDx(), diver.location.getY()+dir.getDy());
			if(dest.equals(boat))
				utility_accumulators.put(dir, 0.0);
		}
		return utility_accumulators;
	}
}
